/**
 * @author monirul
 */

var PlayerDeckModel =  Class.create({
	
	initialize: function(){
		this._id;
		this._playerName;
		this._roleCard;
		this._characterCard;
		this._role;
		this._character;
		this._lifeCount;
		this._cardCount;
		this._isActive=0;
		this._isAttacked;
		this._componentId;
		this._parentComponentId;
//		this._versionNo;
		this._player;
		this._isSelfPlayer;
		this._cardList = [];
		this._cardDeckList = [];
		this._isStarted;
		this._activeCards; 
		this._isReceiveAttack;
		this._isDoDropCard;
		this._isDoPolicy;
		this._attackAbleUsers = [];
		this._dropCardCount;
		this._versionNo = 0;
	},
	
	init:function(o){
		if(o['componentId'] != null){
			this._componentId = o['componentId'];
		}
		if(o['parentComponentId'] != null){
			this._componentId= o['componentId'];
		}
		if(o['playerId'] != null){
			this._id= o['playerId'];
		}
		if(o['player'] != null){
			this._player= o['player'];
		}
		if(o['slefPlayer'] != null){
			this._isSelfPlayer= o['slefPlayer'];
		}
		this.playerDeskUpdated = new Event(this);
		this.playerDeskInitialized = new Event(this);
		this.playerRoleInitialized = new Event(this);
		this.playerCharacterInitialized = new Event(this);
		this.playerCardListUpdated= new Event(this);
		this.playerLifeCountUpdated= new Event(this);
		this.gameReadyToStart = new Event(this);
		this.gameCardDroped = new Event(this);
		
//		this.lifeCountUpdated = new Event(this);
//		this.lifeCountUpdated = new Event(this);
	},
	
	update: function(player) {
		this._player = player;
		this._lifeCount = this._player.blood;
		if(this._isSelfPlayer){
			this._roleCard = this._player.RoleCard;
		}
		this._roleCard = this._player.RoleCard; 
		this.playerDeskUpdated.notify({'player': this._player});
	},
	inititalizePlayerDesk: function(player) {
		this._player = player;
		this._playerName = player.name; 
		this._lifeCount = this._player.blood;
		this._cardCount = 4;//this._player.gameCards.length;
		this.playerDeskInitialized.notify({'player': this._player});
	},
	
	updateRole:function(roleCard){
		this._roleCard = roleCard;
		this._role = roleCard[0].name;
		this.playerRoleInitialized.notify({});
	},
	
	updateCharacter:function(characterCard){
		this._characterCard = characterCard;
		this._character = characterCard.name;
		
		this.playerCharacterInitialized.notify({});
		
	},
	updateLifeCount:function(lifeCount){
		this._lifeCount = _lifeCount;
		this.playerLifeCountUpdated.notify({});
	},
	updateCardList:function(cardList){
		if(this._cardList || this._cardList.length ==0){
			this._cardList = cardList;
			for ( var i = 0; i < this._cardList.length; i++) {
				this._cardDeckList.push(this._createGameCardModel(this._cardList[i]));
			}
		}else{
			for ( var i = 0; i < this._cardList.length; i++) {
				this._cardList.push(this._cardList[i]);
				this._cardDeckList.push(this._createGameCardModel(this._cardList[i]));
			}
		}	
		this.playerCardListUpdated.notify({'cardList':cardList});
	},
	
	_createGameCardModel:function(card){
		var gameCard = new GameCardModel();
		gameCard.init(card);
		return gameCard;
	},
	readyToStart:function(isStart){
		this._isStarted = isStart;
		this.gameReadyToStart.notify({});
	},
	getPlayerName:function(){
		return this._playerName;
	},
	
	getPlayer:function(){
		return this._player;
	},
	
	getRole:function(){
		return this._role;
	},
	
	getCharacter:function(){
		return this._character;
	},
	getCardList:function(){
		return this._cardList;
	},
	
	getCardModelList:function(){
		return this._cardDeckList;
	},
	
	getLifeCount:function(){
		return this._lifeCount;
	},
	
	
	setRole:function(role){
		return this._roleCard.name;
	},
	
	setCharacter:function(character){
		return this._characterCard.name;
	},
	
	setLifeCount:function(lifeCount){
		return this._lifeCount;
	},
	
	getCardCount:function(){
		return this._cardCount;
	},
	
	isActive:function(){
		return this._isActive;
	},
	
	
	getComponentId:function(){
		return this._componentId;
	},
	
	getPlayerId:function(){
		return this._id;
	},
	
	setActive:function(isActive){
		this._isActive = isActive;
	},
	
	finishPlay:function(){
		if(this._isReceiveAttack){
			this.failToDoMatchCard();
			return false;
        }
        else{
        	if(this._isDoDropCard){
				//this.dropCards();
        		return false;
        	}
        	else{
//	        	if(this.isDoPolicy){
//	        		this.isDoPolicy = false;
//					for(var j=0; j<this.selectedCards.length; j++){
//						var element = document.getElementById(this.selectedCards[j].id);
//						element.style.border = "";
//					}
//					this.selectedCards = [];
//	        	}
//			    var room = ComponentManager.getComponentById(this.roomId);
        		return true;
				//room.finishPlay(this.id);
        	}
			
        }
	},
	
	failToDoMatchCard : function(){
		this._isReceiveAttack = false;
//		var room = ComponentManager.getComponentById(this.roomId);
//		var dataVersion = room.getDataVersion();
//		$('send_data_version').innerHTML = dataVersion;
//		RoomService.failToMatchCard(this._id, this.receivedCard);
	},
	
	setActiveCards:function(activeCards){
		this._activeCards = activeCards;
		this._isActive = true;
		var isActive = false;
		for ( var i = 0; i < this._cardDeckList.length; i++) {
			isActive = false;
			var gameCard = this._cardDeckList[i];
			for(var j=0; j<this._activeCards.length; j++){
				if(gameCard.getCardType() == this._activeCards[j]){
					isActive = true;
					break;
				}
			}	
			gameCard.setActive(isActive);
		}
	},
	
	registerAttackableUser : function(userList){
		for(var i=0; i<userList.length; i++){
			this._attackAbleUsers.push(userList[i].id);
		}
	
//		for( var i=0; i<this.attackUser.length; i++){
//			var user = this.attackUser[i];
//			//register DD
//			ddManager.registerDDContainer(user + "_card_area",
//				["attack_1"], {}, this._callbackAttackCardDDEnd.bind(this, user));
//		}
	},
	
	
	selectDropCards : function(dropCount){
		this._dropCardCount = dropCount;
		alert("please select " + this._dropCardCount + " drop cards!");
		this._isDoDropCard = true;
		this.setActive(true);
		if(this._dropCardCount == 0){
			this.dropCards();
		}
	},
	
	okClicked : function(){
		if(this._isDoPolicy){
			//this.discardPolicyCards();
			//this.StopTheClock();
		}
		else if(this._isDoDropCard ){
			var selectedCardsToDrop = [];
			if(this._dropCardCount > 0){
				for ( var i = 0; i < this._cardDeckList.length; i++) {
					var card = this._cardDeckList[i];
					if(card.isSelected()){
						selectedCardsToDrop.push(card);
					}
					
				}
				if(selectedCardsToDrop || selectedCardsToDrop.length == this._dropCardCount ){
					this.dropCards(selectedCardsToDrop);
//					this.StopTheClock();
				}else{
					alert("please select " + this._dropCardCount + " drop cards!");
					return;
				}
			
			}
		}
		else{
//			if(this.selectedCard){
//				if(this.selectedCard.cardType == "arm")	{
//					this.equipCard();
//				}
//				else if(this.isReceiveAttack && this.selectedCard.title == "defend"){
//					var receiveDiv = "user1_card_receive_area";
//					var conf = this.selectedCard.conf;
//					conf.parentId = receiveDiv;
//					var card = new ObjectCard();
//					card.init(conf);
//					card.drawUsedCard();
//					this.discard(this.beSentUser, card);
//					//redraw cards area
//					this.removeAndrefreshCards(this.selectedCard.id, true);
//					//this.clearSelectedStyle();
//					this.isReceiveAttack = false;
//					this.StopTheClock();
//				}
//				else if(this.selectedCard.title == "milk"){
//					if(this.lifeCount < 5){
//						var receiveDiv = "user1_card_receive_area";
//						var conf = this.selectedCard.conf;
//						conf.parentId = receiveDiv;
//						var card = new ObjectCard();
//						card.init(conf);
//						card.drawUsedCard();
//						this.discard(this.user.name, card);
//						//this.addLife();
//						this.removeAndrefreshCards(this.selectedCard.id, true);
//						//this.clearSelectedStyle();
//					}
//				}
//			}
		}
	},
	
	dropCards : function(cards){
		if(!cards){
			cards = [];
			for(var i=0; i<this._dropCardCount; i++){
				var card = this._cardDeckList[i];
				cards.push(card);
			}	
		}
		var dCards = [];
		for(var i=0; i<cards.length; i++){
			dCards.push(cards[i].getCardId());
		}
		RoomService.sendDropCards(this._componentId,dCards, this._versionNo);
//		this.gameCardDroped.notify({});

	},
	finishDropCard:function(){
		for ( var i = 0; i < this._cardDeckList.length; i++) {
			var card = this._cardDeckList[i];
			if(card.isSelected()){
				this._cardDeckList.splice(i,1);
				i--;
			}
		}
		this.gameCardDroped.notify({});
		this._isDoDropCard = false;
	},
	
	cancelClicked : function(){
//		this.clearSelectedStyle();
//		this.StopTheClock();
//		if(this.isReceiveAttack){
//			//this.cutLife();
//			this.failToDoMatchCard();
//		}
//		$('character_policy').style.border="";
	}
	
	
});
